package edu.cmu.cs.stage3.alice.authoringtool.datatransfer;

import java.awt.datatransfer.DataFlavor;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;

public class ElementReferenceTransferable implements java.awt.datatransfer.Transferable {

 // these are for convenience
 public final static java.awt.datatransfer.DataFlavor elementReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Element.class );
 public final static java.awt.datatransfer.DataFlavor expressionReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Expression.class );
 public final static java.awt.datatransfer.DataFlavor questionReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Question.class );
 public final static java.awt.datatransfer.DataFlavor responseReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Response.class );
 public final static java.awt.datatransfer.DataFlavor callToUserDefinedQuestionReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.question.userdefined.CallToUserDefinedQuestion.class );
 public final static java.awt.datatransfer.DataFlavor callToUserDefinedResponseReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.response.CallToUserDefinedResponse.class );
 public final static java.awt.datatransfer.DataFlavor behaviorReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Behavior.class );
 public final static java.awt.datatransfer.DataFlavor transformableReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Transformable.class );
 public final static java.awt.datatransfer.DataFlavor variableReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.Variable.class );
 public final static java.awt.datatransfer.DataFlavor worldReferenceFlavor = AuthoringToolResources.getReferenceFlavorForClass( edu.cmu.cs.stage3.alice.core.World.class );

 protected java.awt.datatransfer.DataFlavor[] flavors;
 protected edu.cmu.cs.stage3.alice.core.Element element;

 public edu.cmu.cs.stage3.alice.core.Element getElement () { return element; }

 public ElementReferenceTransferable (edu.cmu.cs.stage3.alice.core.Element element) {
  // try {String str=""; System.out.println(str.charAt(3));}catch(Exception e) {e.printStackTrace();}
//A  System.out.println("ElementReferenceTransferable.java: "+element);
  this.element = element;

  java.util.HashSet assignables = new java.util.HashSet();
  if (element != null) {
   AuthoringToolResources.findAssignables( element.getClass(), assignables, true );
  } else {
   AuthoringToolResources.findAssignables( edu.cmu.cs.stage3.alice.core.Element.class, assignables, true );
   }

  flavors = new java.awt.datatransfer.DataFlavor[assignables.size() + 1];
  int i = 0;
  for (java.util.Iterator iter=assignables.iterator(); iter.hasNext(); ) {
   flavors[i++] = AuthoringToolResources.getReferenceFlavorForClass( (Class)iter.next() );
   }

  flavors[i++] = java.awt.datatransfer.DataFlavor.stringFlavor;
  }

 public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors () {
  return flavors;
  }

 public boolean isDataFlavorSupported (java.awt.datatransfer.DataFlavor flavor) {
  //LOB for (DataFlavor flavor2 : flavors)
  int t_tam = flavors!=null ? flavors.length : 0;
  DataFlavor flavor2;
  for (int i_i=0; i_i<t_tam; i_i++) { //L
   flavor2 = flavors[i_i]; //L
   if (flavor.equals(flavor2)) {
    return true;
    }
   }
  return false;
  }

 public Object getTransferData (java.awt.datatransfer.DataFlavor flavor) throws java.awt.datatransfer.UnsupportedFlavorException, java.io.IOException {
  if (flavor.equals( java.awt.datatransfer.DataFlavor.stringFlavor )) {
   return element.toString();
  } else if (flavor.getRepresentationClass().isAssignableFrom( element.getClass() )) {
   return element;
  } else {
   throw new java.awt.datatransfer.UnsupportedFlavorException( flavor );
   }
  }

 public ElementReferenceTransferable createCopy () {
  if (element != null) {
   edu.cmu.cs.stage3.alice.core.Element copy = element.createCopyNamed( element.name.getStringValue() );
   return new ElementReferenceTransferable( copy );
  } else {
   return null;
   }
  }

 }
